home *** CD-ROM | disk | FTP | other *** search
- {-------------------------------------------------------}
- { HEXUTIL.INC }
- { Hexadecimal conversion routines }
-
- { Copyright (c) 1987, Ciarcia's Circuit Cellar }
- { All Rights Reserved }
-
- TYPE
-
- Hextype = STRING[4]; { input & output string }
-
-
- {-------------------------------------------------------}
- { Converts a hex string of some hexits into the }
- { corresponding integer. }
- { Returns error code in case the numbers were bad }
-
- PROCEDURE HexToInt(hexits : Hextype;
- VAR intval : INTEGER;
- VAR errcode : INTEGER);
-
- BEGIN
-
- Val('$'+hexits,intval,errcode);
-
- END;
-
-
-
- {-------------------------------------------------------}
- { convert "unsigned" 16-bit number to REAL 0..64K }
-
- FUNCTION WordToReal(fullword:INTEGER) : REAL;
-
- BEGIN
-
- IF fullword < 0
- THEN WordToReal := 65536.0 + fullword
- ELSE WordToReal := fullword;
-
- END;
-
-
- {-------------------------------------------------------}
- { converts one 4-bit nybble to a character }
-
- FUNCTION NybToHex(nybval:INTEGER) : HexType;
-
- BEGIN
- CASE nybval OF
- 0..9 : NybToHex := Chr($30+nybval);
- 10..15 : NybToHex := Chr($41+nybval-10);
- END; { CASE }
- END;
-
-
-
- {-------------------------------------------------------}
- { converts a 16-bit number to characters }
-
- FUNCTION IntToHex(intval:INTEGER) : Hextype;
-
- VAR
- tempstring : Hextype;
- tempval : INTEGER;
-
- BEGIN
-
- tempstring := '0000';
-
- {--- the first nybble depends on the sign }
-
- IF intval < 0 { < 0, need trick }
- THEN BEGIN
- tempval := intval XOR $FFFF; { 1's compl }
- tempval := tempval DIV 4096; { bits to LSD }
- tempval := tempval XOR $000F; { flip again }
- tempstring[1] := NybToHex(tempval); { now convert }
- END
- ELSE BEGIN { >= 0, simple convert }
- tempstring[1] := NybToHex(
- (intval AND $F000) DIV 4096);
- END;
-
- {--- the rest are easy }
-
- tempstring[2] := NybToHex((intval AND $0F00) DIV 256);
- tempstring[3] := NybToHex((intval AND $00F0) DIV 16);
- tempstring[4] := NybToHex( intval AND $000F);
-
- IntToHex := tempstring;
-
- END;
-
-
- {-------------------------------------------------------}
- { converts byte to characters }
-
- FUNCTION ByteToHex(intval:INTEGER) : Hextype;
-
- VAR
- tempstring : Hextype;
-
- BEGIN
-
- tempstring := '00';
-
- tempstring[1] := NybToHex((intval AND $00F0) DIV 16);
- tempstring[2] := NybToHex( intval AND $000F);
-
- ByteToHex := tempstring;
-
- END;
-